package com.changgou.test.lock;

public class waitTea {

    public static void main(String[] args) {
        threadWait threadWait = new threadWait();
        new Thread(()->{
            for (int i=0;i<10;i++){
                try {
                    threadWait.decr();
                } catch (Exception e){

                }
            }
        },"a").start();

        new Thread(()->{
            for (int i=0;i<10;i++){
                try {
                    threadWait.incr();
                } catch (Exception e){

                }
            }
        },"b").start();

    }



}
class  threadWait {

    private int nums =0;

    public synchronized void incr () throws InterruptedException{
        while (nums  != 0){
            this.wait();
        }
        nums++;
        System.out.println(Thread.currentThread().getName()+"-->"+nums);
        this.notify();
    }

    public synchronized  void decr () throws InterruptedException {
        while (nums == 0) {
            this.wait();
        }
        nums--;
        System.out.println(Thread.currentThread().getName()+"-->"+nums);
        this.notify();
    }
}